function ifObject(data) {
	return typeof data === 'object' && !Array.isArray(data) && data !== null;
}

export default {
	// 输出表单项错误（内置）
	outputItemError_(item, errors = []) {
		if (typeof item.outputError === 'function') {
			item.outputError(errors);
		} else {
			item.errors = errors
			item.error = errors[0] || ''
		}
	},
	// 校验表单项格式
	checkItemData(item, isOutput) {
		let errors = [];
		if (typeof item.checkData === 'function') {
			errors = item
				.checkData()
				.map(i => {
					return i();
				})
				.filter(i => {
					if (i) return i;
				});
		}
		if (isOutput) this.outputItemError_(item, errors);
		return errors;
	},
	// 校验表单格式
	checkListData(list, isOutput) {
		let Errors = []
		for (let i in list) {
			let item = list[i];
			if (item.offCheck) continue;
			if (item.offCheckData) continue;
			let errors = this.checkItemData(item, isOutput);
			if (errors.length) Errors[i] = errors;
		}
		return Errors;
	},
	// 校验表单项数据
	async checkItemDataAsync(item, isOutput) {
		let errors = [];
		if (typeof item.checkDataAsync === 'function') {
			errors = await Promise.all(item.checkDataAsync());
			errors = errors.filter(i => {
				if (i) return i;
			});
		}
		if (isOutput) this.outputItemError_(item, errors);
		return errors;
	},
	// 校验表单数据
	checkListDataAsync(list, isOutput) {
		return new Promise(async reslove => {
			let Errors = []
			let pros = [];
			for (let i in list) {
				let item = list[i];
				if (item.offCheck) continue;
				if (item.offCheckDataAsync) continue;
				pros.push(this.checkItemDataAsync(item, isOutput).then(errors => {
					if (errors.length) Errors[i] = errors;
				}))
			}
			await Promise.all(pros)
			reslove(Errors)
		});
	},
	// 写入表单项数据
	setItemData(item, data, isByKey) {
		if (typeof item.setData === 'function') {
			item.setData(data);
		} else {
			if (!ifObject(item.data)) item.data = {};
			if (isByKey) {
				if (typeof item.key !== 'string') item.key = '';
				if (data[item.key] !== undefined) item.data = data[item.key];
			} else {
				for (let i in item.data) {
					if (data[i] !== undefined) item.data[i] = data[i]
				}
			}
		}
	},
	// 写入表单数据
	setListData(list, data, isByKey) {
		for (let i of list) {
			this.setItemData(i, data, isByKey)
		}
	},
	// 获取表单项数据
	getItemData(item, isByKey) {
		let data = {}
		if (typeof item.getData === 'function') {
			data = item.getData(isByKey)
		} else {
			if (!ifObject(item.data)) item.data = {};
			if (isByKey) {
				if (typeof item.key !== 'string') item.key = '';
				data[item.key] = JSON.parse(JSON.stringify(item.data))
				return data;
			} else {
				data = JSON.parse(JSON.stringify(item.data))
			}
		}
		return data;
	},
	// 获取表单数据
	getListData(list, isByKey) {
		let data = {};
		for (let i of list) {
			data = {
				...data,
				...this.getItemData(i, isByKey)
			};
		}
		return data;
	},
	// 清空表单项数据
	resetItemData(item) {
		if (typeof item.resetData === 'function') {
			item.resetData();
		} else {
			if (!ifObject(item.data)) item.data = {}
			for (let i in item.data) {
				if (ifObject(item.data[i])) {
					item.data[i] = {}
				} else if (Array.isArray(item.data[i])) {
					item.data[i] = []
				} else {
					item.data[i] = ''
				}
			}
		}
	},
	// 清空表单数据
	resetListData(list) {
		for (let i of list) {
			this.resetItemData(i)
		}
	}
}
